<?php
namespace Home\Controller;

use Think\Controller;
header("Content-type:text/html;Charset=utf-8");

class UserController extends Controller
{
    /*用户登录*/
    public function login()
    {
        if(isset($_GET['quit'])){
            cookie('token',null);
            cookie('token1',null);
            $this->redirect('user/login','',1,'您已退出');
    }
        $this->display();
    }


    /*用户注册*/
    public function register()
    {
        $this->display();
    }


    /*显示所有发布已通过的文章*/
    public function listarticle(){
        if(isset($_COOKIE['token'])){
            $token=$_COOKIE['token'];
            $token1=$_COOKIE['token1'];
            if(isset($_SESSION[$token])){
                $id=$_SESSION[$token];/*获取cookie token中id值*/
                $mail=$_SESSION[$token1];/*获取用户的mail*/
                $user['id']=$id;/*用户id*/
                $user['mail']=$mail;/*用户注册邮箱*/
                $this->assign('user',$user);
                $article=M('user_addarticle');
                $case['is_pass']=1;
                $alist=$article->where($case)->select();/*筛选所有发表审核通过文章*/
                
				$count=count($alist);/*符合条件记录数*/
				$this->assign('alist',$alist);
				$this->assign('count',$count);
                /*分页管理*/
                /*$User = M('user_addarticle'); // 实例化User对象
                $count  = $User->count();// 查询满足要求的总记录数
                $Page  = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
                $show  = $Page->show();// 分页显示输出
				// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
                $list = $User->limit($Page->firstRow.','.$Page->listRows)->select();
                $this->assign('list',$list);// 赋值数据集
                $this->assign('page',$show);// 赋值分页输出*/
                $this->display(); // 输出模板
            }else{
                $this->redirect('User/login','',1,'未登录');
            }
        }else{
            $this->redirect('User/login','',1,'未登录');
        }
    }


    /*显示审核通过文章*/
    public function showarticle(){
       if(isset($_COOKIE['token'])){
            $token=$_COOKIE['token'];
            $token1=$_COOKIE['token1'];
            if(isset($_SESSION[$token])){
                $id=$_SESSION[$token];/*获取cookie token中id值*/
                $mail=$_SESSION[$token1];/*获取用户的mail*/
                $user['id']=$id;/*用户id*/
                $user['mail']=$mail;/*用户注册邮箱*/
                $this->assign('user',$user);
                $article=M('user_addarticle');
                $case['uid']=$id;
                $list=$article->where($case)->select();/*筛选用户个人发表审核通过文章*/
                $this->assign('list',$list);

                $this->display();
            }else{
                $this->redirect('User/login','',1,'未登录');
            }
        }else{
            $this->redirect('User/login','',1,'未登录');
        }
    }

    /*添加文章*/
    public function article(){
        if(isset($_COOKIE['token'])){
            $token=$_COOKIE['token'];
            $token1=$_COOKIE['token1'];
            if(isset($_SESSION[$token])){
                $id=$_SESSION[$token];
                $mail=$_SESSION[$token1];
                $user['uid']=$id;
                $user['mail']=$mail;
                $this->assign('user',$user);
                $this->display();
            }else{
                $this->redirect('User/login','',1,'未登录');}
        }else{
            $this->redirect('User/login','',1,'未登录');
        }
    }

    /*修改文章*/
    public function changeart(){
        if(isset($_COOKIE['token'])){
            $token=$_COOKIE['token'];
            $token1=$_COOKIE['token1'];
            if(isset($_SESSION[$token])){
               if(isset($_GET['aid'])){
                   $id=$_SESSION[$token];
                   $mail=$_SESSION[$token1];
                   $user['id']=$id;
                   $user['mail']=$mail;
                   $aid['id']=$_GET['aid'];
                   $this->assign('user',$user);
                   $art_c=M('user_addarticle');
                   $arr=$art_c->where($aid)->find();
                   $this->assign('arr',$arr);
                   $this->display();
               }
            }else{
                $this->redirect('User/login','',1,'未登录');}
        }else{
            $this->redirect('User/login','',1,'未登录');
        }
    }

    /*删除文章*/
    public function adel(){
            if(isset($_GET['aid'])){
                $id=$_GET['aid'];/*获取文章编号*/
                $user=M('user_addarticle');
                $aid['id']=$id;
                $user->where($aid)->delete();
                $this->redirect('User/showarticle','',1,'删除成功');
            }
    }

}